<?php

namespace App\Admin\Repositories;

use App\Models\AdminUser;
use App\Models\User as Model;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Http\Client\Request;

class User extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public static function getUserTitleOptions()
    {
        return [
            '1' => '住院医师',
            '2' => '主治医师',
            '3' => '副主任医师',
            '4' => '主任医师',
        ];
    }
    public static function getUnitLevelOptions()
    {
        return [
            '1' => '一级',
            '2' => '二级',
            '3' => '三级',
        ];
    }
    public function get(Grid\Model $model)
    {
        $params = request()->input();
        $admin = Admin::user();

        // 超级管理员看所有

        // 公司的项目经理看自己公司内的所有医生
          if ($admin->isRole('project-manager')) {
            // 查询自己区域的代理人id, 'admin_id' => $admin->id
            $agent = AdminUser::where(['company_id' => $admin->company_id, 'status' => 1])->pluck('id');

            $model->whereIn('agent', $agent);
        }

        // 区域经理看自己上传的专家

        // 如果是区域经理  查看本区域下代理人创建的用户
        if ($admin->isRole('regional-supervisor')) {
            // 查询自己区域的代理人id, 'admin_id' => $admin->id
            $agent = AdminUser::where(['company_id' => $admin->company_id, 'region' => $admin->region, 'status' => 1])->pluck('id');

            $model->whereIn('agent', $agent);
        }
        // 如果是代理人的话查看自己创建的用户
        if ($admin->isRole('agent')) {
            $model->where('agent', $admin->id)->where('company_id', $admin->company_id);
        }
        if (isset($params['per_page']) && !empty($params['per_page'])) {
            $per_page = $params['per_page'];
        } else {
            $per_page = 20;
        }
        // if (!empty($projectID)) {
        //     // return [];
        //     $model->where('project_id', $projectID);
        // }
        $model->limit($per_page);

        return parent::get($model); // TODO: Change the autogenerated stub
    }
}
